/******************************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Ltd.                       */
/*                                                                            */
/*  NAME      = fix_comm.c                                                    */
/*  NOTE      = TDS 请求解析                                                  */
/*  DATE      = 2015/03/19 by zhengxd                                         */
/******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <stdint.h>
#include <limits.h>

#include "fix_core.h"
#include "fix_fast.h"
#include "fix_comm.h"

#define FIX_REPORT_KEY_DBNAME     APM_REPORT_KEY_SESSION".fix.dbname"

/*******************************************************************************
*  FUNC     :  调试用
*  ARGS     :  void
*  RTN      :  void
*  NOTE     :
*******************************************************************************/
void fix_dbg(void)
{
    return;
}

/*******************************************************************************
*  FUNC     :  释放内存
*  ARGS     :  pp_buffer
*  RTN      :  void
*  NOTE     :
*******************************************************************************/
void fix_free(char **pp_buffer)
{
    if (pp_buffer == NULL || *pp_buffer == NULL)
    {
        return;
    }

    frfree(*pp_buffer);
    *pp_buffer = NULL;
    return;
}

/*******************************************************************************
*  FUNC     :  节点node free
*  ARGS     :  pp_buffer
*  RTN      :  void
*  NOTE     :
*******************************************************************************/
void fix_param_node_free(Frlist_node *p_st_node)
{
    Frbindata *p_st_value = NULL;

    if (p_st_node == NULL || p_st_node->data == NULL)
    {
        return;
    }

    Fix_param *p_st_param = (Fix_param *)p_st_node->data;
    if (p_st_param->p_tag_name)
    {
        frfree(p_st_param->p_tag_name);
    }
    /* 释放数据 */
    if (p_st_param->ui_tag == FIX_TAG_RAWDATA)
    {
        p_st_value = p_st_param->field_value.p_st_value;
        if (p_st_value)
        {
            if (p_st_value->p_data)
            {
                frfree(p_st_value->p_data);
            }
            frfree(p_st_value);
        }
    }
    else
    {
        if (p_st_param->field_value.p_value)
        {
            frfree(p_st_param->field_value.p_value);
        }
    }

    frfree(p_st_param);
    return;
}

/*******************************************************************************
*  FUNC     :  字符串转数字
*  ARGS     :  p_inbuff (入力字符串)
*           :  p_ui_len (转换后的数字)
*  RTN      :  FR_FAIL      (FAIL)
*              FR_SUCC      (SUCC)
*  NOTE     :
*******************************************************************************/
int fix_atoui(char *p_inbuff, unsigned int *p_ui_len)
{
    if (p_inbuff == NULL)
    {
        return 0;
    }

    int i_tmp = 0;
    unsigned int ui_len = 0;

    /* 装换成数值 */
    while (*p_inbuff)
    {
        i_tmp = *p_inbuff - '0';
        if (i_tmp > 9 || i_tmp < 0)
        {
            return FR_FAIL;
        }
        ui_len = ui_len * 10 + i_tmp;
        p_inbuff++;
    }

    *p_ui_len = ui_len;
    return FR_SUCC;
}

/*******************************************************************************
*  FUNC     :  解析key/value键值对
*  ARGS     :  pp_inbuff (入力buffer)
*           :  p_ui_in_size (入力buffer的size)
*           :  p_ui_tag (取得的tag)
*           :  pp_value (取得的value)
*  RTN      :  FR_FAIL      (FAIL)
*              FR_SUCC      (SUCC)
*  NOTE     :
*******************************************************************************/
int fix_get_tag(char **pp_inbuff
               , unsigned int *p_ui_in_size
               , unsigned int *p_ui_tag)
{
    unsigned int ui_loop = 0;
    int i_tmp = 0;
    unsigned int ui_tag = 0;
    unsigned int ui_len = 0;
    unsigned int ui_in_size = *p_ui_in_size;
    char *p_inbuff = *pp_inbuff;
    char *p_tmp = NULL;
    char arr_key[12] = {0};

    /* 取得value */
    p_tmp = (char *)memchr(p_inbuff, '=', ui_in_size);
    if (p_tmp == NULL)
    {
        frdbg("not found '=' in the buffer!\n");
        return FR_FAIL;
    }
    ui_len = p_tmp - p_inbuff;
    /* tag都是int型数值 */
    if (ui_len == 0 || ui_len >= 12)
    {
        frdbg("format is incorrect!\n");
        return FR_FAIL;
    }
    snprintf(arr_key, ui_len + 1, "%s", p_inbuff);
    /* 装换成数值 */
    for (ui_loop = 0; ui_loop < ui_len; ui_loop++)
    {
        i_tmp = arr_key[ui_loop] - '0';
        if (i_tmp > 9 || i_tmp < 0)
        {
            frdbg("format is incorrect!\n");
            return FR_FAIL;
        }
        ui_tag = ui_tag * 10 + i_tmp;
    }

    p_inbuff = p_tmp + 1;
    ui_in_size -= ui_len + 1;

    /* 设置出力 */
    *p_ui_tag = ui_tag;
    *p_ui_in_size = ui_in_size;
    *pp_inbuff = p_inbuff;
    return FR_SUCC;
}

/*******************************************************************************
*  FUNC     :  解析key/value键值对
*  ARGS     :  pp_inbuff (入力buffer)
*           :  p_ui_in_size (入力buffer的size)
*           :  p_ui_tag (取得的tag)
*           :  pp_value (取得的value)
*  RTN      :  FR_FAIL      (FAIL)
*              FR_SUCC      (SUCC)
*  NOTE     :
*******************************************************************************/
int fix_skip_value(char **pp_inbuff, unsigned int *p_ui_in_size)
{
    unsigned int ui_len = 0;
    unsigned int ui_in_size = *p_ui_in_size;
    char *p_inbuff = *pp_inbuff;
    char *p_tmp = NULL;

    /* 取得value */
    p_tmp = (char *)memchr(p_inbuff, 0x01, ui_in_size);
    if (p_tmp == NULL)
    {
        frdbg("not found '0x01'(<SOH>) in the buffer!\n");
        return FR_FAIL;
    }
    ui_len = p_tmp - p_inbuff;
    /* 数据为NULL */
    if (ui_len == 0)
    {
        frdbg("value is null!\n");
        /* 设置出力 */
        *p_ui_in_size = ui_in_size - 1;
        *pp_inbuff = p_tmp + 1;
        return FR_SUCC;
    }

    p_inbuff = p_tmp + 1;
    ui_in_size -= ui_len + 1;

    /* 设置出力 */
    *p_ui_in_size = ui_in_size;
    *pp_inbuff = p_inbuff;
    return FR_SUCC;
}

/*******************************************************************************
*  FUNC     :  解析key/value键值对
*  ARGS     :  pp_inbuff (入力buffer)
*           :  p_ui_in_size (入力buffer的size)
*           :  p_ui_tag (取得的tag)
*           :  pp_value (取得的value)
*  RTN      :  FR_FAIL      (FAIL)
*              FR_SUCC      (SUCC)
*  NOTE     :
*******************************************************************************/
int fix_get_value(char **pp_inbuff
                  , unsigned int *p_ui_in_size
                  , char **pp_value)
{
    unsigned int ui_len = 0;
    unsigned int ui_in_size = *p_ui_in_size;
    char *p_inbuff = *pp_inbuff;
    char *p_tmp = NULL;
    char *p_value = NULL;

    /* 取得value */
    p_tmp = (char *)memchr(p_inbuff, 0x01, ui_in_size);
    if (p_tmp == NULL)
    {
        frdbg("not found '0x01'(<SOH>) in the buffer!\n");
        return FR_FAIL;
    }
    ui_len = p_tmp - p_inbuff;
    /* 数据为NULL */
    if (ui_len == 0)
    {
        frdbg("value is null!\n");
        /* 设置出力 */
        *pp_value = NULL;
        *p_ui_in_size = ui_in_size - 1;
        *pp_inbuff = p_tmp + 1;
        return FR_SUCC;
    }

    p_value = (char *)frmalloc(ui_len + 1);
    if (p_value == NULL)
    {
        frdbg("malloc failed!\n");
        return FR_FAIL;
    }
    snprintf(p_value, ui_len + 1, "%s", p_inbuff);
    p_inbuff = p_tmp + 1;
    ui_in_size -= ui_len + 1;

    /* 设置出力 */
    *pp_value = p_value;
    *p_ui_in_size = ui_in_size;
    *pp_inbuff = p_inbuff;
    return FR_SUCC;
}

/*******************************************************************************
*  FUNC     :  解析key/value键值对
*  ARGS     :  pp_inbuff (入力buffer)
*           :  p_ui_in_size (入力buffer的size)
*           :  p_ui_tag (取得的tag)
*           :  pp_value (取得的value)
*  RTN      :  FR_FAIL      (FAIL)
*              FR_SUCC      (SUCC)
*  NOTE     :
*******************************************************************************/
int fix_get_tag_value(char **pp_inbuff
                      , unsigned int *p_ui_in_size
                      , unsigned int *p_ui_tag
                      , char **pp_value)
{
    int i_ret =FR_SUCC;
    unsigned int ui_tag = 0;
    unsigned int ui_len = 0;
    unsigned int ui_in_size = *p_ui_in_size;
    char *p_inbuff = *pp_inbuff;
    char *p_tmp = NULL;
    char *p_value = NULL;
    char arr_key[12] = {0};

    /* 取得value */
    p_tmp = (char *)memchr(p_inbuff, '=', ui_in_size);
    if (p_tmp == NULL)
    {
        frdbg("not found '=' in the buffer!\n");
        return FR_FAIL;
    }
    ui_len = p_tmp - p_inbuff;
    /* tag都是int型数值 */
    if (ui_len == 0 || ui_len >= 12)
    {
        frdbg("format is incorrect!\n");
        return FR_FAIL;
    }
    snprintf(arr_key, ui_len + 1, "%s", p_inbuff);
    i_ret = fix_atoui(arr_key, &ui_tag);
    if (i_ret != FR_SUCC)
    {
        frdbg("change to digit failed!\n");
        return FR_FAIL;
    }

    p_inbuff = p_tmp + 1;
    ui_in_size -= ui_len + 1;
    /* 取得value */
    p_tmp = (char *)memchr(p_inbuff, 0x01, ui_in_size);
    if (p_tmp == NULL)
    {
        frdbg("not found '0x01'(<SOH>) in the buffer!\n");
        return FR_FAIL;
    }
    ui_len = p_tmp - p_inbuff;
    /* 数据为NULL */
    if (ui_len == 0)
    {
        frdbg("value is null!\n");
        /* 设置出力 */
        *p_ui_tag = ui_tag;
        *pp_value = NULL;
        *p_ui_in_size = ui_in_size - 1;
        *pp_inbuff = p_tmp + 1;
        return FR_SUCC;
    }

    p_value = (char *)frmalloc(ui_len + 1);
    if (p_value == NULL)
    {
        frdbg("malloc failed!\n");
        return FR_FAIL;
    }
    snprintf(p_value, ui_len + 1, "%s", p_inbuff);
    p_inbuff = p_tmp + 1;
    ui_in_size -= ui_len + 1;

    /* 设置出力 */
    *p_ui_tag = ui_tag;
    *pp_value = p_value;
    *p_ui_in_size = ui_in_size;
    *pp_inbuff = p_inbuff;
    return FR_SUCC;
}

/*******************************************************************************
*  FUNC     :  解析key/value键值对
*  ARGS     :  pp_inbuff (入力buffer)
*           :  p_ui_in_size (入力buffer的size)
*           :  p_ui_tag (取得的tag)
*           :  pp_value (取得的value)
*           :  ui_value_size (value的size)
*  RTN      :  FR_FAIL      (FAIL)
*              FR_SUCC      (SUCC)
*  NOTE     :
*******************************************************************************/
int fix_skip_value2(char **pp_inbuff
                  , unsigned int *p_ui_in_size
                  , unsigned int ui_value_size)
{
    unsigned int ui_in_size = *p_ui_in_size;
    char *p_inbuff = *pp_inbuff;

    /* 取得value */
    if (ui_in_size <= ui_value_size
        || p_inbuff[ui_value_size] != 0x01)
    {
        frdbg("format is incorrect!\n");
        return FR_FAIL;
    }

    if (ui_value_size == 0)
    {
        frdbg("value is null!\n");
        /* 设置出力 */
        *p_ui_in_size = ui_in_size - 1;
        *pp_inbuff = p_inbuff + 1;
        return FR_SUCC;
    }

    p_inbuff += ui_in_size + 1;
    ui_in_size -= ui_value_size + 1;

    /* 设置出力 */
    *p_ui_in_size = ui_in_size;
    *pp_inbuff = p_inbuff;
    return FR_SUCC;
}

/*******************************************************************************
*  FUNC     :  解析key/value键值对
*  ARGS     :  pp_inbuff (入力buffer)
*           :  p_ui_in_size (入力buffer的size)
*           :  p_ui_tag (取得的tag)
*           :  pp_value (取得的value)
*           :  ui_value_size (value的size)
*  RTN      :  FR_FAIL      (FAIL)
*              FR_SUCC      (SUCC)
*  NOTE     :
*******************************************************************************/
int fix_get_value2(char **pp_inbuff
                  , unsigned int *p_ui_in_size
                  , char **pp_value
                  , unsigned int ui_value_size)
{
    unsigned int ui_in_size = *p_ui_in_size;
    char *p_inbuff = *pp_inbuff;
    char *p_value = NULL;

    /* 取得value */
    if (ui_in_size <= ui_value_size
        || p_inbuff[ui_value_size] != 0x01)
    {
        frdbg("format is incorrect!\n");
        return FR_FAIL;
    }

    if (ui_value_size == 0)
    {
        frdbg("value is null!\n");
        /* 设置出力 */
        *pp_value = NULL;
        *p_ui_in_size = ui_in_size - 1;
        *pp_inbuff = p_inbuff + 1;
        return FR_SUCC;
    }

    p_value = (char *)frmalloc(ui_value_size + 1);
    if (p_value == NULL)
    {
        frdbg("malloc failed!\n");
        return FR_FAIL;
    }
    memcpy(p_value, p_inbuff, ui_value_size);
    p_value[ui_value_size] = '\0';
    p_inbuff += ui_value_size + 1;
    ui_in_size -= ui_value_size + 1;

    /* 设置出力 */
    *pp_value = p_value;
    *p_ui_in_size = ui_in_size;
    *pp_inbuff = p_inbuff;
    return FR_SUCC;
}

/*******************************************************************************
*  FUNC     :  解析key/value键值对
*  ARGS     :  pp_inbuff (入力buffer)
*           :  p_ui_in_size (入力buffer的size)
*           :  p_ui_tag (取得的tag)
*           :  pp_value (取得的value)
*           :  ui_value_size (value的size)
*  RTN      :  FR_FAIL      (FAIL)
*              FR_SUCC      (SUCC)
*  NOTE     :
*******************************************************************************/
int fix_get_tag_value2(char **pp_inbuff
                      , unsigned int *p_ui_in_size
                      , unsigned int *p_ui_tag
                      , char **pp_value
                      , unsigned int ui_value_size)
{
    unsigned int ui_loop = 0;
    int i_tmp = 0;
    unsigned int ui_tag = 0;
    unsigned int ui_len = 0;
    unsigned int ui_in_size = *p_ui_in_size;
    char *p_inbuff = *pp_inbuff;
    char *p_tmp = NULL;
    char *p_value = NULL;
    char arr_key[12] = {0};

    /* 取得value */
    p_tmp = (char *)memchr(p_inbuff, '=', ui_in_size);
    if (p_tmp == NULL)
    {
        frdbg("not found '=' in the buffer!\n");
        return FR_FAIL;
    }
    ui_len = p_tmp - p_inbuff;
    /* tag都是int型数值 */
    if (ui_len == 0 || ui_len >= 12)
    {
        frdbg("format is incorrect!\n");
        return FR_FAIL;
    }
    snprintf(arr_key, ui_len + 1, "%s", p_inbuff);
    /* 装换成数值 */
    for (ui_loop = 0; ui_loop < ui_len; ui_loop++)
    {
        i_tmp = arr_key[ui_loop] - '0';
        if (i_tmp > 9 || i_tmp < 0)
        {
            frdbg("format is incorrect!\n");
            return FR_FAIL;
        }
        ui_tag = ui_tag * 10 + i_tmp;
    }

    p_inbuff = p_tmp + 1;
    ui_in_size -= ui_len + 1;
    /* 取得value */
    if (ui_in_size <= ui_value_size
        || p_inbuff[ui_value_size] != 0x01)
    {
        frdbg("format is incorrect!\n");
        return FR_FAIL;
    }

    if (ui_value_size == 0)
    {
        frdbg("value is null!\n");
        /* 设置出力 */
        *p_ui_tag = ui_tag;
        *pp_value = NULL;
        *p_ui_in_size = ui_in_size - 1;
        *pp_inbuff = p_tmp + 1;
        return FR_SUCC;
    }

    p_value = (char *)frmalloc(ui_value_size + 1);
    if (p_value == NULL)
    {
        frdbg("malloc failed!\n");
        return FR_FAIL;
    }
    memcpy(p_value, p_inbuff, ui_value_size);
    p_value[ui_value_size] = '\0';
    p_inbuff = p_tmp + 1;
    ui_in_size -= ui_value_size + 1;

    /* 设置出力 */
    *p_ui_tag = ui_tag;
    *pp_value = p_value;
    *p_ui_in_size = ui_in_size;
    *pp_inbuff = p_inbuff;
    return FR_SUCC;
}

/*******************************************************************************
*  FUNC     :  解析message header
*  ARGS     :  p_inbuff (入力buffer)
*           :  ui_inbuff_size (入力buffer的size)
*           :  p_ui_hd_len (出力头部长度)
*           :  p_ui_bd_len (出力体部长度)
*           :  pp_version (取得的版本号)
*           :  pp_msg_type (取得message type)
*  RTN      :  FR_FAIL      (FAIL)
*              FR_SUCC      (SUCC)
*  NOTE     :
*******************************************************************************/
int fix_parse_header(char *p_inbuff
                    , unsigned int ui_inbuff_size
                    , unsigned int *p_ui_hd_len
                    , unsigned int *p_ui_bd_len
                    , char **pp_version
                    , char **pp_msg_type)
{
    int i_ret = FR_SUCC;
    unsigned int ui_bd_len = 0;
    unsigned int ui_tmp = ui_inbuff_size;
    unsigned int ui_tmp1 = 0;
    unsigned int ui_tag = 0;
    char *p_version = NULL;
    char *p_body_len = NULL;
    char *p_msg_type = NULL;

    /* 取得message header用以判断是否是FIX */
    i_ret = fix_get_tag_value(&p_inbuff, &ui_inbuff_size, &ui_tag, &p_version);
    if (i_ret != FR_SUCC)
    {
        frdbg("get version failed!\n");
        return FR_FAIL;
    }
    if (ui_tag != FIX_TAG_BEGINSTRING)
    {
        fix_free(&p_version);
        frdbg("get version failed!\n");
        return FR_FAIL;
    }

    /* 取得body length */
    i_ret = fix_get_tag_value(&p_inbuff, &ui_inbuff_size, &ui_tag, &p_body_len);
    if (i_ret != FR_SUCC)
    {
        frdbg("get body length failed!\n");
        fix_free(&p_version);
        return FR_FAIL;
    }
    if (ui_tag != FIX_TAG_BODYLENGTH)
    {
        fix_free(&p_body_len);
        frdbg("get body length failed!\n");
        fix_free(&p_version);
        return FR_FAIL;
    }
    /* 装换成数值 */
    i_ret = fix_atoui(p_body_len, &ui_bd_len);
    fix_free(&p_body_len);
    if (i_ret != FR_SUCC)
    {
        frdbg("get body length failed!\n");
        fix_free(&p_version);
        return FR_FAIL;
    }
    ui_tmp1 = ui_inbuff_size;
    frfree(p_body_len);

    /* 取得message type */
    i_ret = fix_get_tag_value(&p_inbuff, &ui_inbuff_size, &ui_tag, &p_msg_type);
    if (i_ret != FR_SUCC)
    {
        frdbg("get message type failed!\n");
        fix_free(&p_version);
        return FR_FAIL;
    }
    if (ui_tag != FIX_TAG_MSGTYPE)
    {
        fix_free(&p_msg_type);
        frdbg("get message type failed!\n");
        fix_free(&p_version);
        return FR_FAIL;
    }

   *p_ui_hd_len = ui_tmp - ui_inbuff_size;
   *p_ui_bd_len = ui_bd_len - ui_tmp1 + ui_inbuff_size;
   *pp_version = p_version;
   *pp_msg_type = p_msg_type;
   return FR_SUCC;
}

/*******************************************************************************
*  FUNC     :  初始化fragment
*  ARGS     :  p_key (取得的key)
*           :  p_value (取得value)
*           :  ui_size (取得value size)
*           :  p_st_header (list header)
*  RTN      :  FR_FAIL      (FAIL)
*              FR_SUCC      (SUCC)
*  NOTE     :  p_st_hd必须不是NULL
*******************************************************************************/
int fix_create_node(unsigned int ui_tag
                   , char **pp_value
                   , unsigned int ui_size
                   , Frlist_header *p_st_hd)
{
    Frlist_node *p_st_node = NULL;
    Fix_param *p_st_data = NULL;
    Frbindata *p_st_value = NULL;

    p_st_data = (Fix_param *)frmalloc(sizeof(Fix_param));
    if (p_st_data == NULL)
    {
        return FR_FAIL;
    }
    p_st_node = frlist_node_create(p_st_data);
    if (p_st_node == NULL)
    {
        frfree(p_st_data);
        return FR_FAIL;
    }
    p_st_data->p_tag_name = NULL;
    p_st_data->ui_tag = ui_tag;
    switch (ui_tag)
    {
        case FIX_TAG_RAWDATA:
            p_st_value = (Frbindata *)frmalloc(sizeof(Frbindata));
            if (p_st_value == NULL)
            {
                frfree(p_st_data);
                frfree(p_st_node);
                return FR_FAIL;
            }
            p_st_data->field_value.p_st_value = p_st_value;
            p_st_data->field_value.p_st_value->p_data = *pp_value;
            p_st_data->field_value.p_st_value->ui_len = ui_size;
            break;

        default:
            p_st_data->field_value.p_value = *pp_value;
            break;
    }

    *pp_value = NULL;
    frlist_push(p_st_hd, p_st_node);
    return FR_SUCC;
}

/*******************************************************************************
*  FUNC     :  初始化fragment
*  ARGS     :  p_st_fragment (fragment)
*  RTN      :  void
*  NOTE     :  p_st_fragment必须不是NULL
*******************************************************************************/
void fix_init_fragment(L7_fragment_fix *p_st_fragment)
{
    if (p_st_fragment->i_new_flg == FR_YES)
    {
        frfree(p_st_fragment->p_input_msg);
    }
    fix_free(&p_st_fragment->p_msg_type);
    memset(p_st_fragment, 0x0, sizeof(L7_fragment_fix));
    p_st_fragment->i_new_flg = FR_NO;

    return;
}

/*******************************************************************************
*  FUNC     :  销毁fragment
*  ARGS     :  p_st_fragment (fragment)
*  RTN      :  void
*  NOTE     :
*******************************************************************************/
void fix_destroy_fragment(L7_fragment_fix **pp_st_fragment)
{
    if (pp_st_fragment == NULL || *pp_st_fragment == NULL)
    {
        return;
    }

    L7_fragment_fix *p_st_fragment = *pp_st_fragment;

    if (p_st_fragment->i_new_flg == FR_YES)
    {
        frfree(p_st_fragment->p_input_msg);
    }
    fix_free(&p_st_fragment->p_msg_type);
    frfree(p_st_fragment);

    *pp_st_fragment = NULL;
    return;
}

/*******************************************************************************
*  FUNC     :  销毁session
*  ARGS     :  pp_st_session (fragment)
*  RTN      :  void
*  NOTE     :
*******************************************************************************/
void fix_destroy_session(L7session_fix **pp_st_session)
{
    if (pp_st_session == NULL || *pp_st_session == NULL)
    {
        return;
    }

    L7session_fix *p_st_session = *pp_st_session;
    if (p_st_session->p_user_name)
    {
        frfree(p_st_session->p_user_name);
    }

    frlist_remove_all(&p_st_session->st_ua3201, fix_fast_mod_node_free);
    frlist_remove_all(&p_st_session->st_ua3202, fix_fast_mod_node_free);
    frlist_remove_all(&p_st_session->st_ua3107, fix_fast_mod_node_free);
    frlist_remove_all(&p_st_session->st_ua3113, fix_fast_mod_node_free);
    frlist_remove_all(&p_st_session->st_ua3115, fix_fast_mod_node_free);
    frlist_remove_all(&p_st_session->st_ua001, fix_fast_mod_node_free);
    frlist_remove_all(&p_st_session->st_ua002, fix_fast_mod_node_free);
    frlist_remove_all(&p_st_session->st_ua003, fix_fast_mod_node_free);
    frlist_remove_all(&p_st_session->st_ua004, fix_fast_mod_node_free);
    frlist_remove_all(&p_st_session->st_j, fix_fast_mod_node_free);
    frlist_remove_all(&p_st_session->st_f, fix_fast_mod_node_free);
    frlist_remove_all(&p_st_session->st_B, fix_fast_mod_node_free);
    frlist_remove_all(&p_st_session->st_W, fix_fast_mod_node_free);
    frlist_remove_all(&p_st_session->st_ua201, fix_fast_mod_node_free);
    frlist_remove_all(&p_st_session->st_ua202, fix_fast_mod_node_free);

    frfree(p_st_session);

    *pp_st_session = NULL;
    return;
}

/*******************************************************************************
*  FUNC     :  销毁request
*  ARGS     :  pp_st_req (request)
*  RTN      :  void
*  NOTE     :
*******************************************************************************/
void fix_destroy_request(L7_req_fix **pp_st_req)
{

    if (pp_st_req == NULL || *pp_st_req == NULL)
    {
        return;
    }

    L7_req_fix *p_st_req = *pp_st_req;

    fix_free(&p_st_req->p_msg_type);
    frlist_remove_all(&p_st_req->st_param, fix_param_node_free);
    frfree(p_st_req);
    *pp_st_req = NULL;

    return;
}

/*******************************************************************************
*  FUNC     :  销毁response
*  ARGS     :  pp_st_req (request)
*  RTN      :  void
*  NOTE     :
*******************************************************************************/
void fix_destroy_response(L7_resp_fix **pp_st_resp)
{
    if (pp_st_resp == NULL || *pp_st_resp == NULL)
    {
        return;
    }

    L7_resp_fix *p_st_resp = *pp_st_resp;

    frlist_remove_all(&p_st_resp->st_param, fix_param_node_free);
    frfree(p_st_resp);
    *pp_st_resp = NULL;

    return;
}

/*******************************************************************************
*  FUNC     :  初始化私有FIX session
*  ARGS     :  p_st_private(私有session数据)
*  RTN      :  FR_SUCC       (normal)
*              FR_FAIL       (error)
*  NOTE     :  p_st_private必须不是NULL
*******************************************************************************/
void fix_destroy_curr_info(L7session_fix_private *p_st_private)
{
    if (p_st_private->p_st_req)
    {
        fix_destroy_request(&p_st_private->p_st_req);
    }
    if (p_st_private->p_st_resp)
    {
        fix_destroy_response(&p_st_private->p_st_resp);
    }
    fix_init_fragment(p_st_private->p_st_fragment);

    return;
}

/*******************************************************************************
*  FUNC     :  取得时间差(微秒)
*  ARGS     :  st_start_time(开始时间)
*           :  p_st_end_time(结束时间)
*  RTN      :  long
*  NOTE     :
*******************************************************************************/
unsigned long fix_get_interval_time(struct timeval *p_st_start_time
                                     , struct timeval *p_st_end_time)
{
    unsigned long ul_time_interval = 0;
    int i_flg = 0;

    if (p_st_start_time->tv_usec < p_st_end_time->tv_usec)
    {
        ul_time_interval = p_st_end_time->tv_usec - p_st_start_time->tv_usec;
    }
    else
    {
        ul_time_interval = 1000000 + p_st_end_time->tv_usec - p_st_start_time->tv_usec;
        i_flg = 1;
    }

    if (p_st_start_time->tv_sec + i_flg > p_st_end_time->tv_sec)
    {
        ul_time_interval = 0;
    }
    else
    {
        ul_time_interval += 1000000 * (p_st_end_time->tv_sec - p_st_start_time->tv_sec - i_flg);
    }

    return ul_time_interval;
}
